<?php
namespace app\index\model;
use think\Exception;
use think\Model;
use think\Session;

class Draw extends Model
{
	protected $resultSetType = 'collection';
    protected $autoWriteTimestamp = 'datetime';
    protected $updateTime= false;
	//计算未处理的提现信息数量
	public function number(){
	    $res=$this->where("status",0)->field('count(id) as sum')->find();
	    return $res['sum'];
    }
    //查询所有未处理的提现数据
    public function AllData(){
        $res=$this->alias('a')
            ->join('admin','admin.id=a.adminid','left')
            ->join('user','user.id=a.userid','left')
            ->where('status',0)
            ->field('user.name,admin.account as adminname,a.*')
            ->select();
        return $res;
    }
    //提现申请同意时
    public function agrees($id){
	    //改变状态
	    $res=$this->where('id',$id)->update(['status'=>1,'update_time'=>date('Y-m-d H:i:s',time())]);
        //加提现总额度
        $type=$this->where('id',$id)->find();
        if($type['power']==0){
            //给业务员加
            $data=$this->alias('a')
                ->join('admin','admin.id=a.adminid','left')
                ->where('admin.id',$type['adminid'])
                ->field('admin.account,draw')
                ->find();
            zsgc("同意了业务员【".$data['account']."】提现".$data['draw']."元的审核");
            $t=new Admin();
            $arr=$t->where('id',$type['adminid'])->update(['draw'=>(floatval($data['draw'])+floatval($type['money']))]);
        }else{
            //给团长||代理商||用户加
            $data=$this->alias('a')
                ->join('user','user.id=a.userid','left')
                ->where('user.id',$type['userid'])
                ->field('user.name,draw')
                ->find();
            switch ($type['power']){
                case 1:zsgc("同意了团长【".$data['name']."】提现".$data['draw']."元的审核");break;
                case 2:zsgc("同意了代理商【".$data['name']."】提现".$data['draw']."元的审核");break;
                case 3:zsgc("同意了用户【".$data['name']."】提现".$data['draw']."元的审核");break;
            }
            $t=new User();
            $arr=$t->where('id',$type['userid'])->update(['draw'=>(floatval($data['draw'])+floatval($type['money']))]);
        }
        return $arr;
    }
    //提现申请驳回时
    public function reject($id){
	    //改变状态
        $res=$this->where('id',$id)->update(['status'=>2,'update_time'=>date('Y-m-d H:i:s',time())]);
        //把钱返回到账户上
        $type=$this->where('id',$id)->find();
        if($type['power']==0){
            //给业务员返钱
            $data=$this->alias('a')
                ->join('admin','admin.id=a.adminid','left')
                ->where('admin.id',$type['adminid'])
                ->field('admin.money,admin.account')
                ->find();
            zsgc("驳回了业务员【".$data['account']."】提现".$data['money']."元的审核");
            $t=new Admin();
            $arr=$t->where('id',$type['adminid'])->update(['money'=>(floatval($data['money'])+floatval($type['money']))]);
        }else{
            //给团长||代理商||用户返钱
            $data=$this->alias('a')
                ->join('user','user.id=a.userid','left')
                ->where('user.id',$type['userid'])
                ->field('user.money,user.name')
                ->find();
            //存储操作记录
            switch ($type['power']){
                case 1:zsgc("驳回了团长【".$data['name']."】提现".$data['money']."元的审核");break;
                case 2:zsgc("驳回了代理商【".$data['name']."】提现".$data['money']."元的审核");break;
                case 3:zsgc("驳回了用户【".$data['name']."】提现".$data['money']."元的审核");break;
            }
            $t=new User();
            $arr=$t->where('id',$type['userid'])->update(['money'=>(floatval($data['money'])+floatval($type['money']))]);
        }
        return $arr;
    }
    public static function getRoyaltys(){
        $Tid=Session::get('id');
        $re=self::where('adminid',$Tid)->select();
        return $re;
    }
    public static function addRoyalty($data){
        $re=self::create([
            'adminid'=>Session::get('id'),
            'power'=>0,
            'money'=>$data['money'],
            'account'=>$data['account'],
            'accounttype'=>$data['accounttype']
        ]);
        return $re;
    }
    public function getStatusAttr($value){
        $status=[0=>'审核中',1=>'已处理',2=>'驳回'];
        return $status[$value];
    }
    public function getAccounttypeAttr($value){
        $accounttype=[0=>'支付宝',1=>'微信'];
        return $accounttype[$value];
    }
}